//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "common\fn_storeVehicle.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;
private ["_object","_instance_id","_instance_content","_instances"];
DEFAULT_PARAM(_object,0,objNull);
DEFAULT_PARAM(_instance_id,1,"");
if (isServer) then {
	if (isnil QGVAR(STORING_VEHICLE_BUSY)) then { GVAR(STORING_VEHICLE_BUSY) = -1; };
	waituntil {(GVAR(STORING_VEHICLE_BUSY) < time)};
	GVAR(STORING_VEHICLE_BUSY) = time + 10;
	if (_instance_id == "") then {_instance_id = [_object] call FFUNC(GetInstanceID);};
	_instances = OGETVAR(GVAR(mission_store),QGVAR(MISSION_INSTANCES),[]);
	if (!(_instance_id in _instances)) then {
		_instances set [count _instances, _instance_id];
		OSETVAR(GVAR(mission_store),QGVAR(MISSION_INSTANCES),_instances);
	};
	_instance_content = OGETVAR(GVAR(placed_objs_store),_instance_id,[]);
	if (_object in _instance_content) then {
		TRACE_1("Vehicle already stored",_object);
	} else {
		TRACE_1("Adding Vehicle to the store",ARR_3(_object,_instance_id,count _instance_content));
		_instance_content set [ count _instance_content, _object];
		OSETVAR(GVAR(placed_objs_store),_instance_id,_instance_content);
	};
	GVAR(STORING_VEHICLE_BUSY) = -1;
	TRACE("Vehicle Stored",ARR_2(_instance_id, count _instance_content));
} else {
	TRACE("No storing outside of the server");
};
TRACE("End");
TMFPROFILERSTOP;
